package de.koller.aframe.plain;

import de.koller.oceanFramework.gl.util.IImageRaster;
import java.util.Arrays;

public class PlainRasterInt implements IImageRaster {

	public final int width, height;
	public final int[] pixel;

	public PlainRasterInt( int[] pixel, int width, int height ) {
		this.width = width;
		this.height = height;
		this.pixel = pixel;
	}
	
	public PlainRasterInt( int width, int height ) {
		this( new int[width*height], width, height );
	}

	@Override
	public void setColor( int color, float x, float y ) {
		this.setPixel( color, (int)x, (int)y );
	}

	@Override
	public int getColor( float x, float y ) {
		return this.getOffset( (int)x, (int)y );
	}

	@Override
	public int getWidth() {
		return width;
	}

	@Override
	public int getHeight() {
		return height;
	}

	public void clear( int color ) {
		Arrays.fill( pixel, color );
	}
	
	public int getColor( int x, int y ) {
		return getPixel( getOffset( x, y ) );
	}
	
	public void setPixel( int color, int x, int y ) {
		setPixel( color, getOffset( x, y ) );
	}
	
	public int getOffset( int x, int y ) {
		if( x < 0 || y < 0 || x >= width || y >= height )
			return -1;
		return y * width + x;
	}
	
	private int getPixel( int offset ) {
		if( offset == -1 )
			return 0xffffffff;
		return pixel[ offset ];
	}
	
	private void setPixel( int color, int offset ) {
		if( offset == -1 )
			return;
		pixel[ offset ] = color;
	}
	
}
